package com.wzqj.agent.app.merchant.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.app.merchant.dao.AppMerchantStoreDao;
import com.wzqj.agent.app.merchant.domain.MerchantStore;
import com.wzqj.agent.app.merchant.service.ApiMerchantService;
import com.wzqj.agent.common.ConfigHelper;
import com.wzqj.agent.common.SoaConfig;
import com.wzqj.agent.common.exception.BusinessErrorType;
import com.wzqj.agent.common.exception.BusinessException;
import com.wzqj.wsp.encrypt.MD5;
import com.wzqj.wsp.response.ResponseValue;
import com.wzqj.wsp.response.ResultStatus;
import com.wzqj.wsp.soa.SOAUtil;

/**
 * Created by wanghao@weipass.cn on 2015/8/3.
 */
@Service("apiMerchantService")
public class ApiMerchantServiceImpl implements ApiMerchantService {

    /**
     * 商户平台服务
     */
    private static final String WMP_PROVIDER_APP_CODE = ConfigHelper.getProperty("WMP_PROVIDER_APP_CODE");

    /**
     * 注册商户
     */
    private static final String WMP_MERCHANT_REGISTER_URL = "user/webRegUser";

    /**
     * 根据手机号获取商户信息
     */
    private static final String WMP_MERCHANT_SEARCH_URL = "user/getUserByMobile";

    /**
     * 根据资质id获取资质信息
     */
    private static final String WMP_QUALIFICATION_SEARCH_URL = "qualification/getQualificationById";

    /**
     * 添加资质
     */
    private static final String WMP_QUALIFICATION_ADD_URL = "qualification/addOrUpdateQualification";

    /**
     * 绑定店铺到商户
     */
    private static final String WMP_MERCHANT_STORE_BIND_URL = "merchant/bindUserMerchant";

    @Autowired
    private AppMerchantStoreDao merchantStoreDao;
    /**
     * 开通商户参数：商户类型
     */
    private static final int ISADMIN = 1;//1：小微商户
    /**
     * 开通商户参数：商户账户类型
     */
    private static final int USERTYPE = 2;//2：小微商户
    /**
     * 根据手机号获取商户信息
     * @param phone
     * @return
     */
    @Override
    public JSONObject getMerchant(String phone) {

        JSONObject soaParam = SoaConfig.getSoaParam();
        JSONObject params = new JSONObject();
        params.put("mobile", phone);
        ResponseValue res = SOAUtil.getResValFromSOA(WMP_PROVIDER_APP_CODE, WMP_MERCHANT_SEARCH_URL, params,soaParam);
        if (res != null){
        	System.out.println("作业App调取商户平台接口（根据手机号获取商户信息）："+res);
            if(res.getStatus() == ResultStatus.SUCCESS_STATUS){
                return (JSONObject) res.getData();
            }else if(res.getStatus() == ResultStatus.NO_VALUE_STATUS){
                return null;
            }else {
                throw BusinessException.createInstance(BusinessErrorType.MERCHANT_REGISTER_ERROR);
            }
        }else{
            throw BusinessException.createInstance(BusinessErrorType.MERCHANT_REGISTER_ERROR);
        }
    }

    /**
     * 注册商户
     * @param phone
     * @return
     */
    @Override
    public JSONObject register(String phone,String password) throws BusinessException{

        JSONObject soaParam = SoaConfig.getSoaParam();
        String passwordMD5 = MD5.getMD5(password.getBytes()).toLowerCase();
        JSONObject params = new JSONObject();
        params.put("mobile", phone);
        params.put("password", passwordMD5);
        params.put("userType",USERTYPE);
        params.put("isAdmin", ISADMIN);//1代表小微商户	
        ResponseValue res = SOAUtil.getResValFromSOA(WMP_PROVIDER_APP_CODE, WMP_MERCHANT_REGISTER_URL, params,soaParam);
        if (res != null){
            if(res.getStatus() == ResultStatus.SUCCESS_STATUS){
                return (JSONObject) res.getData();
            }else {
                throw BusinessException.createInstance(BusinessErrorType.MERCHANT_REGISTER_ERROR);
            }
        }else{
            throw BusinessException.createInstance(BusinessErrorType.MERCHANT_REGISTER_ERROR);
        }
    }

    /**
     * 根据资质ID获取资质信息
     * @param id
     * @return
     */
    @Override
    public JSONObject getQualificationById(String id){

        JSONObject soaParam = SoaConfig.getSoaParam();
        JSONObject params = new JSONObject();
        params.put("id", id);
        ResponseValue res = SOAUtil.getResValFromSOA(WMP_PROVIDER_APP_CODE, WMP_QUALIFICATION_SEARCH_URL, params,soaParam);
        if (res != null){
            if(res.getStatus() == ResultStatus.SUCCESS_STATUS){
                return (JSONObject) res.getData();
            }else {
                throw BusinessException.createInstance(BusinessErrorType.QUALIFICATION_SEARCH_ERROR);
            }
        }else{
            throw BusinessException.createInstance(BusinessErrorType.QUALIFICATION_SEARCH_ERROR);
        }
    }

    /**
     * 新注册用户资质认证
     * @param phone
     * @param uid
     * @return
     */
    @Override
    public String addQualification(String phone, String uid) {

        JSONObject soaParam = SoaConfig.getSoaParam();
        JSONObject params = new JSONObject();
        params.put("uid", uid);
        params.put("merName",phone);
        params.put("checkStatus",2);
        ResponseValue res = SOAUtil.getResValFromSOA(WMP_PROVIDER_APP_CODE, WMP_QUALIFICATION_ADD_URL, params,soaParam);
        if (res != null){
        	System.out.println("作业App调取商户平台（新注册用户资质认证）："+res);
            if(res.getStatus() == ResultStatus.SUCCESS_STATUS){
                return (String) res.getData();
            }else {
                throw BusinessException.createInstance(BusinessErrorType.MERCHANT_REGISTER_ERROR);
            }
        }else{
            throw BusinessException.createInstance(BusinessErrorType.MERCHANT_REGISTER_ERROR);
        }
    }

    /**
     * 绑定店铺
     * @param mcode
     * @param storeid
     * @param qualification
     * @return
     */
    @Override
    public JSONObject bindStore(String mcode, String storeid, String qualification,Integer claimStatus) {

        JSONObject soaParam = SoaConfig.getSoaParam();
        JSONObject params = new JSONObject();
        params.put("storeId", storeid);
        params.put("mcode", mcode);
        params.put("qualificationId", qualification);
        params.put("claimStatus",claimStatus);
        System.out.println("App绑定店铺接口参数merchant/bindUserMerchant"+params.toString());
        ResponseValue res = SOAUtil.getResValFromSOA(WMP_PROVIDER_APP_CODE, WMP_MERCHANT_STORE_BIND_URL, params,soaParam);
        System.out.println("App绑定店铺接口返回数据:"+res);
        if (res != null){
            if(res.getStatus() == ResultStatus.SUCCESS_STATUS){
                MerchantStore merchantStore = new MerchantStore();
                merchantStore.setMcode(mcode);
                merchantStore.setStoreId(storeid);
                merchantStore.setMerId(qualification);
                merchantStoreDao.insert(merchantStore);
                System.out.println("App绑定店铺数据库操作完成！");
                JSONObject data = new JSONObject();
                data.put("mcode",mcode);
                return data;
            }else {
                throw BusinessException.createInstance(BusinessErrorType.MERCHANT_STORE_BIND_ERROR);
            }
        }else{
            throw BusinessException.createInstance(BusinessErrorType.MERCHANT_STORE_BIND_ERROR);
        }
    }

}
